package jdk8;

/**
 * @author xiaoming
 * @version 1.0
 * @date 2018-10-21 13:55
 *  官方参考: https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
 **/
public class LambdaExpressions {

    public static void main(String[] args) {
      LambdaExpressions lambdaExpressions = new LambdaExpressions();

        /**
         * 实现了接口MathOperation的operation方法
         * 参数类型声明
         */
        MathOperation addition = (int a, int b ) -> { return a + b;};
        /**
         * 不用类型声明
         */
        MathOperation subtraction  = ( a,b ) ->  a -  b;

        System.out.println(lambdaExpressions.operate(10,5,addition));
        System.out.println(lambdaExpressions.operate(10,5,subtraction));

        // 不用λ表达式
        GreetingService greetingService1 = new GreetingService() {
            @Override
            public void sayMessage(String message) {
                System.out.println("传统方式Hello." + message);
            }
        };
        // 参数不用括号
        GreetingService greetingService2 = message -> System.out.println("Hello," + message);

        //参数用括号
        GreetingService greetingService3 = (message) -> System.out.println("Hello," + message);

        greetingService1.sayMessage("XiaoMing");
        greetingService2.sayMessage("XiaoMing");

    }

    interface MathOperation{
        int operation(int a , int b);
    }
    interface GreetingService{
        void sayMessage(String message);
    }
    private int operate(int a,int b, MathOperation mathOperation){
        return mathOperation.operation(a,b);
    }
}
